package com.ssyt.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.company.entity.ApplicationEntity;
import com.ssyt.hiring.company.entity.PositionEntity;
import com.ssyt.hiring.company.service.ApplicationService;
import com.ssyt.hiring.company.service.PositionService;
import com.ssyt.hiring.talent.dao.ResumeDao;
import com.ssyt.hiring.talent.entity.ResumeEntity;

public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionService positionService = new PositionService();
	private ApplicationService applicationService = new ApplicationService();
	private ResumeDao resumeDao = new ResumeDao();

	public PositionServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("toAddUI".equals(command)) {
			toAddUI(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("preupdate".equals(command)) {
			preupdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/***
	 * 查询职位信息并跳转到企业后台
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Constants.getCompanyId(request, response);
		List<PositionEntity> positionList = positionService.listById(id);
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("WEB-INF/views/company/company-main.jsp")
				.forward(request, response);
	}

	/**
	 * 跳转至发布职位页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toAddUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("WEB-INF/views/company/position-add.jsp")
				.forward(request, response);
	}

	/**
	 * 添加职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Constants.getCompanyId(request, response);
		String name = request.getParameter("name");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");

		PositionEntity position = new PositionEntity();
		position.setCompanyId(id);
		position.setName(name);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		position.setDuty(duty);
		position.setAbility(ability);

		int row = positionService.add(position);

		response.sendRedirect("CompanyServlet?command=main&row=" + row);

	}

	/**
	 * 根据ID查询职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	private void preupdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int companyId = Constants.getCompanyId(request, response);
		PositionEntity position = positionService.preupdate(id, companyId);

		if (position == null) {
			request.setAttribute("error", "该职位不存在");

			List<PositionEntity> positionList = positionService
					.listById(companyId);
			request.setAttribute("positionList", positionList);
			request.getRequestDispatcher(
					"/WEB-INF/views/company/company-main.jsp").forward(request,
					response);
		} else {
			request.setAttribute("position", position);
			request.getRequestDispatcher(
					"/WEB-INF/views/company/position-update.jsp").forward(
					request, response);
		}

	}

	/**
	 * 根据ID修改职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");

		PositionEntity position = new PositionEntity();
		position.setId(id);
		position.setName(name);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		position.setDuty(duty);
		position.setAbility(ability);

		int row = positionService.update(position);

		response.sendRedirect("CompanyServlet?command=main&row=" + row);

	}

	/**
	 * 根据ID删除职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));

		int row = positionService.delete(id);

		if (row == 0) {
			response.getWriter().print("fail");
		}
	}

	/**
	 * 查询职位详细信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void detail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (Constants.isCompanyLogin(request)) {
			request.getSession().invalidate();
		}
		if (!Constants.isTalentLogin(request)) {
			request.setAttribute("msg", "请先登录，再执行简历投递！");
			request.getRequestDispatcher("/TalentServlet?command=toLoginUI")
					.forward(request, response);
		} else {
			int id = Integer.parseInt(request.getParameter("id"));
			PositionEntity position = positionService.detail(id);
			int talentId = Constants.getTalentId(request, response);
			ApplicationEntity application = applicationService.isApplied(
					talentId, id);
			List<ResumeEntity> list = resumeDao.resumeList(talentId);
			request.setAttribute("position", position);
			request.setAttribute("application", application);
			request.setAttribute("resumeList", list);
			request.getRequestDispatcher(
					"/WEB-INF/views/company/position-detail.jsp").forward(
					request, response);
		}
	}
}
